//
//  WBDBManager.m
//  WeatherBar
//
//  Created by Bert on 2024/6/7.
//

#import "WBDBManager.h"
#import "NSFileManager+SWYPaths.h"
#import "WBUserManager.h"

@implementation WBDBManager

+ (NSString *)getCurrentAccounDBName
{
    // 使用hash(账户id)为数据库名称
    
    return nil;
}
/// 新建数据库
+ (BOOL)openOrCreateDB:(NSString *)name
{
    bg_setSqliteName([self getDBName:name]);
    return YES;
    
}
+ (NSString *)getDBName:(NSString *)name
{
    NSString *dbName = [NSString stringWithFormat:@"%@!@#%@",[UIDevice wb_getZuid],name];
//    return [THEncryptManager md5String:dbName];
    return dbName;
}
/// 删除数据库
+ (BOOL)deleteDB:(NSString *)name
{
    bg_deleteSqlite([self getDBName:name]);
    
    return YES;
}

+ (BOOL)closeDB:(NSString *)name
{
    bg_closeDB([self getDBName:name]);
    return YES;
}

#pragma mark - 操作数据库
//+ (NSArray *)getDateArray {
//    NSArray *dateArr = [NSArray bg_arrayWithName:kJYDate];
//    return dateArr;
//}
//+ (BOOL)addDateToArray:(NSString *)dateStr {
//    BOOL isSuccess = [NSArray bg_addObjectWithName:kJYDate object:dateStr];
//    return isSuccess;
//}

//搜索记录
+ (NSArray *)getSearchRecordArray {
    NSArray *recordArr = [NSArray bg_arrayWithName:kSearchRecord];
    return recordArr;
}
+ (BOOL)addSearchRecordToArray:(NSString *)string {
    BOOL isSuccess = [NSArray bg_addObjectWithName:kSearchRecord object:string];
    return isSuccess;
}
+ (BOOL)deleteSearchRecordWithIndex:(NSInteger)index {
    BOOL isSuccess = [NSArray bg_deleteObjectWithName:kSearchRecord Index:index];
    return isSuccess;
}


#pragma mark - 归档操作
// 把对象归档存到沙盒里
+ (BOOL)saveObject:(id)object byFileName:(NSString *)fileName {
    NSString *path  = [self appendFilePath:fileName];
    path = [path stringByAppendingString:@".archive"];
    BOOL success = [NSKeyedArchiver archiveRootObject:object toFile:path];
    return success;
    
}

// 通过文件名从沙盒中找到归档的对象
+ (id)getObjectByFileName:(NSString*)fileName {
    NSString *path  = [self appendFilePath:fileName];
    path = [path stringByAppendingString:@".archive"];
    id obj =  [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return obj;
}

// 根据文件名删除沙盒中的文件
+ (void)removeObjectByFileName:(NSString *)fileName {
    NSString *path  = [self appendFilePath:fileName];
    path = [path stringByAppendingString:@".archive"];
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}

+ (NSString *)appendFilename:(NSString *)fileName {
    
    // 1. 沙盒缓存路径
    NSString *cachesPath = [NSFileManager cachesPath];
    if (![[NSFileManager defaultManager] fileExistsAtPath:cachesPath isDirectory:nil]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:cachesPath withIntermediateDirectories:NO attributes:nil error:nil];
    }
    
    return cachesPath;
}

// 拼接文件路径
+ (NSString *)appendFilePath:(NSString *)fileName {
    
    // 1. 沙盒缓存路径
    NSString *cachesPath = [NSFileManager cachesPath];
    
    NSString *filePath = [NSString stringWithFormat:@"%@/%@",cachesPath,fileName];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:nil]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:nil];
    }
    
    return filePath;
}
#pragma mark - 存在document下
// 把对象归档存到沙盒里
//+ (BOOL)saveLocationInDocumentWithObject:(id)object byFilePath:(NSString *)filePath {
//    // 1. 沙盒缓存路径
//    NSString *cachesPath = [NSFileManager documentsPath];
//    NSString *filePath = [NSString stringWithFormat:@"%@/%@",cachesPath,fileName];
//
//    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:nil]) {
//        [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:nil];
//    }
//
//    NSString *filePath = [NSString stringWithFormat:@"%@/%@.archive",path, fileName];
//    //path = [path stringByAppendingString:@".archive"];
//    BOOL success = [NSKeyedArchiver archiveRootObject:object toFile:filePath];
//    NSLog(@"存储的filePath ===== %@",path);
//    return success;
//}

// 拼接文件路径
+ (NSString *)documentAppendFilePath:(NSString *)fileName {
    
    // 1. 沙盒缓存路径
    NSString *documentsPath = [NSFileManager documentsPath];
    
    NSString *filePath = [NSString stringWithFormat:@"%@/%@",documentsPath,fileName];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:nil]) {
        //withIntermediateDirectories是否创建中间目录
        BOOL isSuc = [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
        if (isSuc) {
            return filePath;
        } else {
            return nil;
        }
    } else {
        return filePath;
    }
}
// 把对象归档存到沙盒里,（fileName = 文件名 && fileName = 对象名）
+ (BOOL)saveInDocumentWithObject:(id)object byFileName:(NSString *)fileName {
    NSString *path  = [self documentAppendFilePath:fileName];
    NSString *filePath;
    if ([fileName containsString:@"/"]) {
        NSArray *array = [fileName componentsSeparatedByString:@"/"];
        filePath = [NSString stringWithFormat:@"%@/%@.archive",path, array.lastObject];
    } else {
        filePath = [NSString stringWithFormat:@"%@/%@.archive",path, fileName];
    }
    //path = [path stringByAppendingString:@".archive"];
    BOOL success = [NSKeyedArchiver archiveRootObject:object toFile:filePath];
//    NSLog(@"存储的filePath ===== %@",path);
    return success;
}

// 通过文件名从沙盒中找到归档的对象
+ (id)getInDocumentWithObjectByFileName:(NSString*)fileName {
    NSString *path  = [self documentAppendFilePath:fileName];
    NSString *filePath;
    if ([fileName containsString:@"/"]) {
        NSArray *array = [fileName componentsSeparatedByString:@"/"];
        filePath = [NSString stringWithFormat:@"%@/%@.archive",path, array.lastObject];
    } else {
        filePath = [NSString stringWithFormat:@"%@/%@.archive",path, fileName];
    }
    //path = [path stringByAppendingString:@".archive"];
    id obj =  [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//    NSLog(@"取出的filePath ===== %@",path);
    return obj;
}

// 把对象归档存到沙盒里,（fileName != 文件名 && fileName = 对象名）
+ (BOOL)saveInDocumentWithObject:(id)object filePath:(NSString *)filePath fileName:(NSString *)fileName {
    NSString *path  = [self documentAppendFilePath:filePath];
    filePath = [NSString stringWithFormat:@"%@/%@.archive",path, fileName];
    BOOL success = [NSKeyedArchiver archiveRootObject:object toFile:filePath];
    NSLog(@"存储的filePath ===== %@",path);
    return success;
}

// 通过文件名从沙盒中找到归档的对象
+ (id)getObjectInDocumentWithFilePath:(NSString*)filePath fileName:(NSString*)fileName {
    NSString *path  = [self documentAppendFilePath:filePath];
    filePath = [NSString stringWithFormat:@"%@/%@.archive",path, fileName];
    id obj =  [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"取出的filePath ===== %@",filePath);
    return obj;
}

// 通过文件名从沙盒中找到归档的对象
+ (id)getObjectInDocumentWithFilePath:(NSString*)filePath {
    NSString *path  = [self documentAppendFilePath:filePath];
    id obj =  [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//    NSLog(@"取出的filePath ===== %@",path);
    return obj;
}

// 根据文件名删除沙盒中的文件
+ (void)removeInDocumentWithObjectByFileName:(NSString *)fileName {
    NSString *path  = [self documentAppendFilePath:fileName];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@.archive",path, fileName];
    //path = [path stringByAppendingString:@".archive"];
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
#pragma mark - User Default
// 存储用户偏好设置
+ (void)saveUserData:(id)data forKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
// 存储Bool偏好设置
+ (void)saveBool:(BOOL)value forKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] setBool:value forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

// 读取用户偏好设置
+ (id)readUserDataForKey:(NSString *)key {
    id obj = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return obj;
}
// 读取Bool偏好设置
+ (BOOL)readBoolForKey:(NSString *)key {
    BOOL obj = [[NSUserDefaults standardUserDefaults] boolForKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return obj;
}


// 删除用户偏好设置
+ (void)removeUserDataForkey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
